package com.lmuse.xmr.foodspotting.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.andreabaccega.widget.FormEditText;
import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.model.User;
import com.lmuse.xmr.foodspotting.ui.register.RegisterActivity;
import com.lmuse.xmr.foodspotting.ui.userpage.ForgetPwdActivity;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.UserPresenterImpl;

import java.util.List;

import cn.bmob.v3.exception.BmobException;

public class LoginActivity extends AppCompatActivity implements Contract.UserView{

    private Button login;
    private Button register;
    private FormEditText emailEdit;
    private FormEditText passwordEdit;
    private TextView forgetPwd;

    private InputMethodManager inputMethodManager; //处理隐藏输入法
    private Contract.UserPresenter userPresenter;

    private boolean allValid = true; //输入合法性

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userPresenter = new UserPresenterImpl(this);
        initView();
    }

    void initView() {
        inputMethodManager =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        login = (Button) findViewById(R.id.btn_goto_login);
        register = (Button) findViewById(R.id.btn_goto_register);
        emailEdit = (FormEditText) findViewById(R.id.et_login_email);
        passwordEdit = (FormEditText) findViewById(R.id.et_login_password);
        forgetPwd = (TextView) findViewById(R.id.tv_forget_password);
        login.setOnClickListener(new ButtonClickListener());
        register.setOnClickListener(new ButtonClickListener());
        forgetPwd.setOnClickListener(new ButtonClickListener());
    }

    class ButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_goto_login:
                    checkInputValid();
                    inputMethodManager.hideSoftInputFromWindow(passwordEdit.getWindowToken(), 0);
                    inputMethodManager.hideSoftInputFromWindow(emailEdit.getWindowToken(), 0);
                    break;
                case R.id.btn_goto_register:
                    Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                    startActivity(intent);
                    LoginActivity.this.finish();
                    break;
                case R.id.tv_forget_password:
                    Intent forgetIntent = new Intent(LoginActivity.this, ForgetPwdActivity.class);
                    startActivity(forgetIntent);
                    break;
            }
        }
    }


    /**
     * 输入合法性检查
     */
    void checkInputValid() {
        FormEditText[] allFields = { emailEdit, passwordEdit};

        for (FormEditText field: allFields) {
            allValid = field.testValidity() && allValid;
        }

        if (allValid) {
            userPresenter.signIn(emailEdit.getText().toString(), passwordEdit.getText().toString());
        } else {
            // EditText are going to appear with an exclamation mark and an explicative message.
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        userPresenter.onDetach();
    }

    @Override
    public void onSignUpSuccess(User user) {

    }

    @Override
    public void onSignUpFailed(String msg) {

    }

    @Override
    public void onSignInSuccess(User user) {
        Toast.makeText(this, "登录成功！", Toast.LENGTH_SHORT).show();
        this.finish();
    }

    @Override
    public void onSignInFailed(String msg) {
        Toast.makeText(this, "登录失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUserInfoCache(User user) {

    }

    @Override
    public void onUpdateUserSuccess() {

    }

    @Override
    public void onUpdateUserFailed(String msg) {

    }

    @Override
    public void onRequestEmailVerifyResult(BmobException e) {

    }

    @Override
    public void onResetPasswordByEmailResult(BmobException e) {

    }

    @Override
    public void onQueryUserByIdSuccess(User user) {

    }

    @Override
    public void onQueryUserByIdFailed(String msg) {

    }

    @Override
    public void onQueryUserAmountSuccess(int size) {

    }

    @Override
    public void onQueryUserAmountFailed(String msg) {

    }

    @Override
    public void onQueryByKeySuccess(List<User> userList) {

    }

    @Override
    public void onQueryByKeyFailed(String msg) {

    }
}
